package edu.cuit.avatar.map;

import java.util.HashMap;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/15 13:51
 */
public class HashMapTest {
    public static void main(String[] args) {
        var map = new HashMap<String, Integer>();
        map.put("peppa", 5);
        map.put("jorge", 3);
        //key不允许重复(覆盖)
        map.put("peppa", 6);

        //key不存在,则添加保存;key存在返回旧值,不添加新值
        map.putIfAbsent("pedro", 6);
        System.out.println(map.putIfAbsent("jorge", 4));//3
        System.out.println(map.get("jorge"));//3

        //根据key获取value
        Integer value = map.get("peppa");
        System.out.println(value);
        System.out.println(map.get("emily"));//null
        System.out.println(map.getOrDefault("emily", 10));//10

        //检查key是否存在
        System.out.println(map.containsKey("peppa"));
        //删除entry
        map.remove("peppa");
        map.put(null, null);
        map.put(null, 10);

        map.replace("peppa", 5);//map.put("peppa",5)
        map.replace("candy", 6);

        System.out.println(map);

        HashMapDemo.each(map);
    }
}
